from apps.users.models import User
from rest_framework.serializers import ModelSerializer
from rest_framework import serializers
from apps.article.models import Article
from apps.question.models import Label
from apps.question.models import Question
from apps.question.models import Reply
from apps.recruit.models import Enterprise
from apps.recruit.models import Recruit

class UserModelSerializer(ModelSerializer):
    class Meta:
        model = User
        fields = ['id','username','avatar']
# TODO labels
class LabelModelSerializer(ModelSerializer):

    class Meta:
        model = Label
        fields = ['id','label_name']
# TODO questions
class QuestionModelSerializer(ModelSerializer):
    labels = serializers.StringRelatedField()
    user = serializers.StringRelatedField()
    class Meta:
        model = Question
        fields = '__all__'
# TODO answer_question
class QuestionReplyModelSerializer(ModelSerializer):
    labels = serializers.StringRelatedField()
    user = UserModelSerializer(many=True,read_only=True)
    class Meta:
        model = Question
        fields = '__all__'
class ReplyModelSerializer(ModelSerializer):
    subs = QuestionReplyModelSerializer(many=True,read_only=True)
    user = UserModelSerializer(read_only=True)
    parent = serializers.StringRelatedField()
    class Meta:
        model = Reply
        fields = '__all__'
# TODO enterpises
class RecruitModelSerializer(ModelSerializer):
    class Meta:
        model = Recruit
        fields = ['id']
class EnterpriseModelSerializer(ModelSerializer):
    recruits = RecruitModelSerializer(many=True,read_only=True)
    class Meta:
        model = Enterprise
        fields = ['id','name','lables','logo','summary','recruits']
# TODO collected_articles

class UserCollectedModelSerializer(ModelSerializer):
    class Meta:
        model = User
        fields = ['id']
class ArticlesModelSerializer(ModelSerializer):
    class Meta:
        model = Article
        fields = ['id','title']

class UserArticleModelSerializer(ModelSerializer):
    articles = ArticlesModelSerializer(many=True,read_only=True)
    fans = UserCollectedModelSerializer(many=True,read_only=True)
    class Meta:
        model = User
        fields = ['id','username','avatar','articles','fans']
class ArticleModelSerializer(ModelSerializer):
    user = UserArticleModelSerializer(read_only=True)
    collected_users = UserArticleModelSerializer(many=True,read_only=True)
    collected = serializers.BooleanField(default=False)
    class Meta:
        model = Article
        fields = ['id','title','content','createtime','user',
                  'collected_users','image','collected']

class InfoModelSerializer(ModelSerializer):
    labels = LabelModelSerializer(many=True,read_only=True)
    questions = QuestionModelSerializer(many=True,read_only=True)
    enterpises = EnterpriseModelSerializer(many=True,read_only=True)
    # answer_question 1. (subs -->(user)) 2. user
    replies = ReplyModelSerializer(many=True,read_only=True)
    # collected_articles 1. (user -->(articles)) 2.  fans 3.  collected_users
    collected_articles = ArticleModelSerializer(many=True,read_only=True)
    class Meta:
        model = User
        fields = ["id","username","mobile","realname","birthday","sex",
    "avatar","website","email","city","address","labels","questions",
    "replies","collected_articles","enterpises"]
        extra_kwargs = {
            'username': {'read_only': True},
            'id': {'read_only': True}
        }
class PasswordModelSerializer(ModelSerializer):
    class Meta:
        model = User
        fields = ['password']
        extra_kwargs = {
            'password': {'write_only': True}
        }